﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace calculator1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool endApp = false;
            //Display title as  the C# console calculator app.
            Console.WriteLine("Console calculator in C#\r");
            Console.WriteLine("------------------------\n");

            while (!endApp)
            {
                //Declare variables and set to empty.
                string numInput1 = "";
                string numInput2 = "";
                double result = 0;  

                //Ask the user to type the first number.
                Console.WriteLine("Type a number, and then press Enter.");
                numInput1 = Console.ReadLine(); 

                double cleanNum1 = 0;
                while(!double.TryParse(numInput1, out cleanNum1))
                {
                    Console.Write("This is not valid input, please enter an integer value:");
                    numInput1 = Console.ReadLine();
                }

                //Ask the user to type another number.
                Console.WriteLine("Type another number, and then press Enter");
                numInput2 = Console.ReadLine();

                double cleanNum2 = 0;
                while (!double.TryParse(numInput2, out cleanNum2))
                {
                    Console.Write("This is not valid input, please enter an integer value:");
                    numInput2 = Console.ReadLine();
                }

                //Ask the user to choose an option.
                Console.WriteLine("Choose an option from the following list");
                Console.WriteLine("\ta - Add");
                Console.WriteLine("\tb - Minus");
                Console.WriteLine("\tc - Multiply");
                Console.WriteLine("\td - Divide");
                Console.WriteLine("Your option?");

                string op = Console.ReadLine();

                try
                {
                    result = calculator1.Calculator.DoOperation(cleanNum1, cleanNum2, op);
                    if (double.IsNaN(result))
                    {
                        Console.WriteLine("This operation will result in a mathmetical error");
                    }
                    else Console.WriteLine("Your result: {0:0.##}\n", result);
                }catch(Exception e)
                {
                    Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
                }

                Console.WriteLine("------------------------\n");

                //Wait for the user to respond before closing.
                Console.Write("Press 'n' to close the Calculator console app or press any other key and Enter to continue:");
                if(Console.ReadLine() == "n")
                {
                    endApp = true;
                }
                Console.WriteLine("\n"); //Friendly linespacing.
            }
            return;
        }
    }
    class Calculator
    {
        public static double DoOperation(double num1, double num2, string op)
        {
            double result = double.NaN;//// Default value is "not-a-number" which we use if an operation, such as division, could result in an error.
            //Use a switch statement to do the math.
            switch (op)
            {
                case "a":
                    result = num1 + num2;
                    break;
                case "b":
                    result = num1 - num2;
                    break;
                case "c":
                    result = num1 * num2;
                    break;
                case "d":
                    if (num2 != 0)
                    {
                        result = num1 / num2;
                    }
                    break;
                default:
                    break;
            }
            return result;
        }
    }
}
